package com.xiaoniu.douyinvideo.util

import android.content.SharedPreferences
import android.preference.PreferenceManager
import com.xiaoniu.douyinvideo.MyApplication


/**
 * @类说明： 小数据存储工具类
 */
object PUtil {
    /**
     * path
     */
    const val PATH_ENVIRONMENT: String = "path_environment"

    const val KEY_SHOWED_TIP: String = "key_showed_tip"
    
    private fun preferences(): SharedPreferences {
        return PreferenceManager.getDefaultSharedPreferences(MyApplication.instance)
    }

    fun <T> putPreferences(key: String?, value: T?) {
        val editor: SharedPreferences.Editor = preferences().edit()
        if (value is String) {
            editor.putString(key, value.toString())
        } else if (value is Boolean) {
            editor.putBoolean(key, (value as Boolean))
        } else if (value is Int) {
            editor.putInt(key, (value as Int))
        } else if (value is Float) {
            editor.putFloat(key, (value as Float))
        } else if (value is Long) {
            editor.putLong(key, (value as Long))
        }
        editor.commit()
    }

    fun <T> getPreferences(key: String?, value: T?): T? {
        var o: Any? = null
        if (value is String) {
            o = preferences().getString(key, value.toString())
        } else if (value is Boolean) {
            o = preferences().getBoolean(key, (value as Boolean))
        } else if (value is Int) {
            o = preferences().getInt(key, (value as Int))
        } else if (value is Float) {
            o = preferences().getFloat(key, (value as Float))
        } else if (value is Long) {
            o = preferences().getLong(key, (value as Long))
        }
        val t = o as T?

        return t
    }
}